package 
{
  import away3d.cameras.lenses.PerspectiveLens;
  import away3d.containers.View3D;
  import away3d.core.geom.Plane3D;
  import away3d.core.math.Number3D;
  import away3d.primitives.Plane;
  import away3d.primitives.Sphere;
  import away3d.sprites.MovieClipSprite;
	import flash.display.Sprite;
	import flash.events.Event;
  import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author ivan
	 */
	public class RayPlaneIntersection extends Sprite 
	{
		public function RayPlaneIntersection():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
      
      addChild(view);
      
      sphere.radius = 10;
      view.scene.addChild(sphere);
      
      plane.width = plane.height = 1000;
      view.scene.addChild(plane);
      
      view.camera.lens = new PerspectiveLens(); // The important step I was missing!
      view.camera.y = 100;
      view.camera.lookAt(sphere.position);      
      
      addEventListener(Event.ENTER_FRAME, onEnterFrame);
      addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
		}
    
    private function onEnterFrame(e:Event):void
    { 
      view.render();
    }
    
    private function onMouseMove(e:MouseEvent):void
    {
      var plane:Plane3D = new Plane3D();
      plane.fromNormalAndPoint(new Number3D(0, 1, 0), new Number3D(0, 0, 0));
      
      var ray:Number3D = view.camera.unproject(e.stageX - view.x, e.stageY - view.y);
      ray.add(view.camera.position, ray);
      
      var intersect:Number3D = plane.getIntersectionLineNumbers(view.camera.position, ray);
      
      sphere.position = intersect;
    }
    
    private var view:View3D = new View3D( { x:250, y:120 } );
    private var sphere:Sphere = new Sphere();
    private var plane:Plane = new Plane( {segments:30} );
		
	}
	
}